﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Watson.Core.Tools;
using ITOMMS.Entity;
using ITOMMS.Business;

namespace ITOMMS.Web.InsideManage
{
    public partial class SupplierAdd : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (Request.Params["id"] != null)
                {
                    string sid = Request.Params["id"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        DataShow(pid);
                    }
                }


                if (Request.Params["vid"] != null)
                {
                    string sid = Request.Params["vid"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        DataShow(pid);
                        btnSave.Visible = false;
                    }
                }

            }
        }



        private void DataShow(int troubleId)
        {
            tbSupplierEntity entity = new tbSupplierEntity();
            entity = entity.GetById(troubleId);

            if (entity != null)
            {
                ViewState["Entity"] = entity;

                txt公司名称.Text = entity.公司名称;
                txt编号.Text = entity.编号;
                txt电话.Text = entity.电话;
                txt值真.Text = entity.传真;
               
             
                txt邮政编码.Text = entity.邮政编码;
                txt区域.Text = entity.区域;
                txt公司地址.Text = entity.公司地址;
                txt开户银行.Text = entity.开户银行;
                txt银行账号.Text = entity.银行账号;
                txt税号.Text = entity.税号;
                txt公司经营范围.Text = entity.公司经营范围;
                txt公司代理产品.Text = entity.公司代理产品;
                txt备注.Text = entity.备注;
                this.txt负责人1.Text = entity.负责人1;
                this.txt负责人1QQ.Text = entity.QQ1;
                this.负责人1电话.Text = entity.负责人电话1;
                this.txt负责人1手机.Text = entity.负责人手机1;
                this.txt负责人1MSN.Text = entity.MSN1;
                this.txt负责人1邮箱.Text = entity.邮箱1;
                this.txt负责人2.Text = entity.负责人2;
                this.txt负责人2QQ.Text = entity.QQ2;
                this.txt负责人2电话.Text = entity.负责人电话2;
                this.txt负责人2手机.Text = entity.负责人手机2;
                this.txt负责人2MSN.Text = entity.MSN2;
                this.txt负责人2邮箱.Text = entity.邮箱2;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Entity.tbSupplierEntity entity = new ITOMMS.Entity.tbSupplierEntity();
                if (ViewState["Entity"] != null)
                {
                    entity = ViewState["Entity"] as tbSupplierEntity;
                }
                else
                {
                    entity.CreateBy = SessionHelper.UserID;
                    entity.CreateDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }

                entity.公司名称 = txt公司名称.Text;
                entity.编号 = txt编号.Text;
                entity.电话 = txt电话.Text;
                entity.传真 = txt值真.Text;
             
                entity.负责人1 = this.txt负责人1.Text;
                entity.QQ1 = this.txt负责人1QQ.Text;
                entity.负责人电话1 = this.负责人1电话.Text;
                entity.负责人手机1 = this.txt负责人1手机.Text;
                entity.MSN1 = this.txt负责人1MSN.Text;
                entity.邮箱1 = this.txt负责人1邮箱.Text;
                entity.负责人2 = this.txt负责人2.Text;
                entity.负责人电话2 = this.txt负责人2电话.Text;
                entity.负责人手机2 = this.txt负责人2手机.Text;
                entity.QQ2 = this.txt负责人2QQ.Text;
                entity.MSN2 = this.txt负责人2MSN.Text;
                entity.邮箱2 = this.txt负责人2邮箱.Text;

                entity.邮政编码 = txt邮政编码.Text;
                entity.区域 = txt区域.Text;
                entity.公司地址 = txt公司地址.Text;
                entity.开户银行 = txt开户银行.Text;
                entity.银行账号 = txt银行账号.Text;
                entity.税号 = txt税号.Text;
                entity.公司经营范围 = txt公司经营范围.Text;
                entity.公司代理产品 = txt公司代理产品.Text;
                entity.备注 = txt备注.Text;

                entity.ModifyBy = SessionHelper.UserID;
                entity.ModifyDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));


                entity.SaveOrUpdate(entity);
                ViewState["Entity"] = entity;
                MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "保存成功");
            }
            catch (Exception ex)
            {
                log4netHelper.error(ex);
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "保存失败");
            }

        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("SupplierList.aspx", true);
        }
    }
}
